﻿using System;
using System.Linq;
using System.Web.Mvc.Html;
using System.Windows.Input;
using NTQOnlineExam.Services.EntityFW.Context;
using NTQOnlineExam.Services.EntityFW.Entity;
using NTQOnlineExam.Services.Infrastructure;
using NTQOnlineExam.Services.Infrastructure.Utility;

namespace NTQOnlineExam.Services.CommandService
{
    public class TermCommandService : ITermCommandService
    {
        public bool DeleteTermItem(int termId)
        {
            Guard.Against<ArgumentException>(termId <= 0, "Term Id is required.");
            using (var context = new ExamOnlineDbContext())
            {
                Term term = context.Terms.Find(termId);
                Guard.Against<ApplicationException>(term == null, string.Format("Can not find any term having Id = '{0}'", termId));

                var referencedItems = context.Database.SqlQuery<int>("Select SectionId FROM dbo.Section WHERE TermId = " + termId).FirstOrDefault<int>(); //context.Sections.SqlQuery("select * from dbo.Section Where TermId = " + termId).ToList();
                Guard.Against<ApplicationException>(referencedItems > 0, string.Format("Can not delete the term having Id = '{0}', it's referenced by some sections", termId));

                context.Set<Term>().Remove(term);
                context.SaveChanges();
            }
            return true;
        }

        public bool UpdateTerm(TermSection.UpdateTermCommand command)
        {
            Guard.ArgumentIsNotNull(command, "command");
            Guard.Against<ArgumentException>(command.TermId <= 0, "Term Id is required");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(command.TermName), "Term name can not be empty");
            Guard.Against<ArgumentException>(command.Price.HasValue && command.Price <= 0, "Price can not be less than or equal to zero");

            using (var context = new ExamOnlineDbContext())
            {
                Term term = context.Terms.Find(command.TermId);
                Guard.Against<ApplicationException>(term == null, string.Format("Can not find any term having Id = '{0}'", command.TermId));
                term.TermName = command.TermName;
                term.Description = command.Description;
                term.Price = command.Price;
                term.IsPublished = command.IsPublished;
                term.YearId = command.YearId;

                context.Entry(term).State = System.Data.EntityState.Modified;
                context.SaveChanges();
            }
            return true;
        }

        public int CreateTerm(TermSection.CreateTermCommand command)
        {
            Guard.ArgumentIsNotNull(command, "command");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(command.TermName), "Term name can not be empty");
            Guard.Against<ArgumentException>(command.Price.HasValue && command.Price <= 0, "Price can not be less than or equal to zero");

            using (var context = new ExamOnlineDbContext())
            {
                Term term = new Term
                {
                    TermName = command.TermName,
                    Description = command.Description,
                    Price = command.Price,
                    IsPublished = command.IsPublished,
                    YearId = command.YearId
                };
                context.Set<Term>().Add(term);
                context.SaveChanges();
                return term.TermId;
            }
        }

        public bool DeleteTerm(TermSection.DeleteTermCommand command)
        {
            Guard.ArgumentIsNotNull(command, "command");

            foreach (var id in command.TermId)
            {
                DeleteTermItem(id);
            }
            return true;
        }
    }
}

